package edu.pku.sei.transformation.editor.model;

import java.util.ArrayList;

import org.w3c.dom.Element;

import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.core.MetaPrimitiveType;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.transformation.editor.internal.TransformationEditorModelRoot;
import edu.pku.sei.transformation.editor.model.base.NamedElement;
import edu.pku.sei.transformation.editor.model.base.TypedElement;
import edu.pku.sei.transformation.editor.model.execution.IObjectNode;

public class TypedModel extends TypedElement implements IObjectNode{
	private ArrayList outgoingObjectFlows = new ArrayList();
	private ArrayList incomingObjectFlows = new ArrayList();
	
	public Object[] getTypes() {
		ArrayList list = new ArrayList();
		MetaPackage pkg = TransformationEditorModelRoot.getPrimitivePackage(getModel());
		for (Object object : pkg.getNamedElements()) {
			if (object instanceof MetaPrimitiveType)
				list.add(object);
		}
		pkg = TransformationEditorModelRoot.getModelTypePkg(getModel());
		for (Object object : pkg.getOtherElements()) {
			if (object instanceof ModelType)
				list.add(object);
		}
		return list.toArray();
	}
	@Override
	public String toString() {
		if (type == null)
			return getName()+" : null";
		else if (type instanceof NamedElement) 
			return getName()+":"+((NamedElement)type).getName();
		else
			return getName()+":"+type.toString();
	}
	
	public Transformation getTransformation() {
		return (Transformation) this.container;
	}
	
	public void setTransformation(Transformation trans) {
		this.container = trans;
	}
	@Override
	public ArrayList getIncomings() {
		return this.incomingObjectFlows;
	}
	@Override
	public ArrayList getOutgoings() {
		return this.outgoingObjectFlows;
	}
	
	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodes(root,
				ModelPackage.NODE_OUTGOINGS, this.outgoingObjectFlows);
		XMLUtility.xmlInstance.serializeNodesNotContain(root,
				ModelPackage.NODE_INCOMINGS, this.incomingObjectFlows);
	}

	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.outgoingObjectFlows.addAll(XMLUtility.xmlInstance.deSerializeNodes(root,
				ModelPackage.NODE_OUTGOINGS));
		this.incomingObjectFlows.addAll(XMLUtility.xmlInstance
				.deSerializeNodesNotContain(root, ModelPackage.NODE_INCOMINGS,
						this));
	}

	@Override
	public void dynamicSet(String featureID, Object value) {
		if (ModelPackage.NODE_INCOMINGS.equals(featureID)) {
			this.incomingObjectFlows.add(value);
		} else if (ModelPackage.NODE_OUTGOINGS.equals(featureID)) {
			this.outgoingObjectFlows.add(value);
		} else
			super.dynamicSet(featureID, value);
	}
	@Override
	public Object getData() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean isDataReady() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean pullData() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean pushData() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean receiveData(Object data) {
		// TODO Auto-generated method stub
		return false;
	}

}
